<?php

namespace App\Http\Requests\Backend\Admin;

use App\Rules\MobileCheck;
use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

class AdminRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch($this->method()) {
            /*
             * index
             */
            case 'GET': {
                if ($this->routeIs('admin.index')) {
                    return [
                        'account' => 'max:100',
                    ];
                } else if ($this->routeIs('admin.show')) {
                    return [];
                } else {
                    return [];
                }
            }
            /*
             * store
             */
            case 'POST': {
                return [
                    'account' => 'required|max:100|unique:admins,account',
                    'mobile' => [
                        'required',
                        'max:100',
                        'unique:admins,mobile',
                        new MobileCheck(),
                    ],
                    'email' => 'required|email|unique:admins,email',
                    'post' => 'required|max:100',
                    'role' => 'required|integer|min:1|exists:admin_roles,id',
                ];
            }
            /*
             * update
             */
            case 'PUT': {
                $id = $this->route('id');
                return [
                    'mobile' => [
                        'max:100',
                        new MobileCheck(),
                        Rule::unique('admins', 'mobile')->ignore($id),
                    ],
                    'email' => [
                        'email',
                        Rule::unique('admins', 'email')->ignore($id),
                    ],
                    'post' => 'max:100',
                    'role' => 'required|integer|min:1|exists:admin_roles,id',
                ];
            }
            case 'PATCH': {
                if ($this->routeIs('admin.password')) {
                    return [
                        'password' => 'required',
                        'password_confirm' => 'required',
                    ];
                } else if ($this->routeIs('admin.reset')) {
                    return [];
                } else {
                    return [];
                }
            }
            default: {
                return [];
            }
        }
    }

    /**
     * 验证信息返回
     *
     * @return array
     */
    public function messages()
    {
        return [
            'account.required' => '缺少账户名称',
            'account.max' => '账户名称超出范围',
            'account.unique' => '账户名称重复',
            'mobile.required' => '缺少手机号码',
            'mobile.max' => '手机号码超出范围',
            'mobile.unique' => '手机号码重复',
            'email.required' => '缺少邮箱',
            'email.email' => '邮箱超出范围',
            'email.unique' => '邮箱重复',
            'post.required' => '缺少职务',
            'post.max' => '职务超出范围',
            'role.required' => '缺少角色',
            'role.integer' => '角色必须为整型',
            'role.min' => '角色最小值超出范围',
            'role.exists' => '角色不存在',
            'password.required' => '缺少原密码',
            'password_confirm.required' => '缺少新密码',
        ];
    }
}
